#ifndef GUARD_NAMING_SCREEN_H
#define GUARD_NAMING_SCREEN_H

#include "global.h"
#include "task.h"


#define KBEVENT_NONE 0
#define KBEVENT_PRESSED_A 5
#define KBEVENT_PRESSED_B 6
#define KBEVENT_PRESSED_SELECT 8
#define KBEVENT_PRESSED_START 9

enum
{
    NAMING_SCREEN_PLAYER,
    NAMING_SCREEN_BOX,
    NAMING_SCREEN_CAUGHT_MON,
    NAMING_SCREEN_NAME_RATER,
    NAMING_SCREEN_RIVAL,

	//New
    NAMING_SCREEN_CHOOSE_NUMBER,
	NAMING_SCREEN_ENTER_PHRASE,
	NAMING_SCREEN_ENTER_PASSWORD,
	NAMING_SCREEN_ENTER_5_CHAR_PASSWORD,
	NAMING_SCREEN_ENTER_SPECIES_NAME,
};

struct NamingScreenTemplate
{
    u8 copyExistingString;
    u8 maxChars;
    u8 iconFunction;
    u8 addGenderIcon;
    u8 initialPage;
    const u8 *title;
};

enum {
    WIN_KB_PAGE_1, // Which of these two windows is in front is cycled as the player swaps
    WIN_KB_PAGE_2, // Initially WIN_KB_PAGE_1 is in front, with WIN_KB_PAGE_2 on deck
    WIN_TEXT_ENTRY,
    WIN_TEXT_ENTRY_BOX,
    WIN_BANNER,
    WIN_CH,
    WIN_PINYIN,
    WIN_COUNT,
};

struct NamingScreenData
{
    /*0x1E10*/ u8 state;
    /*0x1E11*/ u8 windows[WIN_COUNT];
    /*0x1E16*/ u16 inputCharBaseXPos;
    /*0x1E18*/ u16 bg1vOffset;
    /*0x1E1A*/ u16 bg2vOffset;
    /*0x1E1C*/ u16 bg1Priority;
    /*0x1E1E*/ u16 bg2Priority;
    /*0x1E20*/ u8 bgToReveal;
    /*0x1E21*/ u8 bgToHide;
    /*0x1E22*/ u8 currentPage;
    /*0x1E23*/ u8 cursorSpriteId;
    u8 keyRole;
    /*0x1E28*/ const struct NamingScreenTemplate *template;
    /*0x1E2C*/ u8 templateNum;
    u8 chBufferCount;
    u8 curChRowMax;
    /*0x1E30*/ u8 *destBuffer;
    /*0x1E34*/ u16 monSpecies;
    /*0x1E36*/ u16 monGender;
    /*0x1E38*/ u32 monPersonality;
    /*0x1E3C*/ MainCallback returnCallback;
    const u8* curChChars;
    u32 chCharsPos;
    u8 gTextMode[16];
    u8 textBuffer[16];
    u8 chBuffer[16];
//    u8 tileBuffer[0x2000];
    u8 tilemapBuffer1[0x800];
    u8 tilemapBuffer2[0x800];
    u8 tilemapBuffer3[0x800];
    u8 rectBuffer[32*16];
    u8 rectBuffer2[64*16];
};

enum
{
    PAGE_OTHERS,
    PAGE_UPPER,
    PAGE_LOWER,
};

enum
{
    MAIN_STATE_BEGIN_FADE_IN,
    MAIN_STATE_WAIT_FADE_IN,
    MAIN_STATE_HANDLE_INPUT,
    MAIN_STATE_MOVE_TO_OK_BUTTON,
    MAIN_STATE_START_PAGE_SWAP,
    MAIN_STATE_WAIT_PAGE_SWAP,
    MAIN_STATE_6,
    MAIN_STATE_UPDATE_SENT_TO_PC_MESSAGE,
    MAIN_STATE_BEGIN_FADE_OUT,
};

enum
{
    INPUT_STATE_DISABLED,
    INPUT_STATE_ENABLED,
    INPUT_STATE_2,
};

u8 __attribute__((long_call)) DoNamingScreen(u8 caseId, u8* dst, u16 monSpecies, u8 monGender, u32 monPersonality, void (*callback)(void));
bool8 __attribute__((long_call)) NameChooserSelectLetter(void);
bool8 __attribute__((long_call)) NamingScreen_TriggerKeyboardChange(void);
u8 __attribute__((long_call)) NamingScreen_GetCharAtKeyboardPos(s16 x, s16 y);
u8 __attribute__((long_call)) GetRivalAvatarGraphicsIdByStateIdAndGender(u8 state, u8 gender);
void __attribute__((long_call)) SeedRngAndSetTrainerId(void);
bool8 __attribute__((long_call)) IsDestinationBoxFull(void);
void __attribute__((long_call)) MultiplyInvertedPaletteRGBComponents(u16 i, u8 r, u8 g, u8 b);
u8 __attribute__((long_call)) CreateObjectGraphicsSprite(u16 graphicsId, SpriteCallback callback, s16 x, s16 y, u8 subpriority);
void __attribute__((long_call)) CopyObjectGraphicsInfoToSpriteTemplate(u16 graphicsId, void (*callback)(struct Sprite *), struct SpriteTemplate *spriteTemplate, const struct SubspriteTable **subspriteTables);
u16* __attribute__((long_call)) GetTextWindowPalette(u8 id);
bool8 __attribute__((long_call)) PageSwapAnimState_2(struct Task *);
bool8 __attribute__((long_call)) PageSwapAnimState_1(struct Task *);
u8 __attribute__((long_call)) CurrentPageToNextKeyboardId(void);
void __attribute__((long_call)) SetCursorInvisibility(u8);

u8* GetBoxNamePtr(u8 boxId);
u8 GetKeyRoleAtCursorPos(void);
bool8 MainState_StartPageSwap(void);
bool8 MainState_WaitPageSwap(void);
void MoveCursorToOKButton(void);
bool8 KeyboardKeyHandler_Page(u8);
void PrintControls(u8 page);
void DrawChineseTextEntry();
void ReDrawChineseTextEntry();
void SetInputState(u8);
void DeleteTextCharacter(void);
u8 GetInputEvent(void);
bool8 KeyboardKeyHandler_OK(u8);
bool8 KeyboardKeyHandler_Backspace(u8);
void CB2_LoadNamingScreen(void);
void NamingScreen_Init(void);
void NamingScreen_InitBGs(void);
void CreateNamingScreenTask(void);
bool8 MainState_FadeIn(void);
void Task_NamingScreen(u8);
void NamingScreen_ShowBgs(void);
void VBlankCB_NamingScreen(void);
void LoadGfx(void);
void LoadPalettes(void);
void CreateSprites(void);
void CreateCursorSprite(void);
void CreateTextEntrySprites(void);
void CreateInputTargetIcon(void);
void SetCursorPos(s16, s16);
bool8 MainState_WaitFadeIn(void);
bool8 HandleKeyboardEvent(void);
bool8 MainState_HandleInput(void);
bool8 MainState_MoveToOKButton(void);
bool8 MainState_PressedOKButton(void);
bool8 MainState_WaitSentToPCMessage(void);
bool8 MainState_FadeOut(void);
bool8 MainState_Exit(void);
void DrawTextEntry(void);
bool8 AddTextCharacter(void);
bool8 IsPageChMode();
bool8 CanDPadMoveToTop();
const u8* GetCurrentPageChChars();
void TurnChinesePage(u8 next);
const u8* FindChChars();
void ClearChiniesTextEntry();
u16 GetCharAtKeyboardPos(s16 x, s16 y);
u8 GetTextEntryPosition(void);
void DrawGenderIcon(void);
void DrawCotrolHightLightByPage(u8 page);
void DrawCotrolHightLight(u8 x, u8 width, u8 type, u8 page);
void ResetVHBlank(void);
void CB2_NamingScreen(void);
void SetVBlank(void);
void SetSpritesVisible(void);
void DecompressToBgTilemapBuffer(u8, const u32 *);
u8 GetCurrentPageColumnCountByRow(s16 y);
u8 GetPreviousTextCaretPosition(void);
void CalcNextChars();
void TryStartButtonFlash(u8, u8, u8);
void CreateButtonFlashTask(void);
void Task_UpdateButtonFlash(u8);
u16 GetButtonPalOffset(u8);
void DisplaySentToPCMessage(void);
void Task_HandlePageSwapAnim(u8);
bool8 IsPageSwapAnimNotInProgress(void);
bool8 PageSwapAnimState_Init(struct Task *);
bool8 PageSwapAnimState_Done(struct Task *);
void SpriteCB_Cursor(struct Sprite *);
void SpriteCB_InputArrow(struct Sprite *);
void SpriteCB_Underscore(struct Sprite *);
void GetCursorPos(s16 *, s16 *);
void SetCursorFlashing(u8);
bool8 IsCursorAnimFinished(void);
void SquishCursor(void);
bool8 KeyboardKeyHandler_Character(u8);
void PrintKeyboardKeys(u8, u8);
void CreateHelperTasks(void);
void CopyStrClear(const u8* src, u8* dest, u8 pad);
void SaveInputText(void);
void DummyGenderIcon(void);
void HandleDpadMovement(struct Task *);
void Input_Enabled(struct Task *);
void Input_Disabled(struct Task *);
void Task_HandleInput(u8);
bool8 SwapKeyboardPage(void);
void NamingScreen_CreateRivalIcon(void);
void NamingScreen_CreateMonIcon(void);
void NamingScreen_CreatePCIcon(void);
void NamingScreen_CreatePlayerIcon(void);
void NamingScreen_NoIcon(void);

struct PingYinSerchNode
{
    const u8* key;
    const u8* val;
};

extern const struct PingYinSerchNode gPingYinSerchNode[416];

#endif // GUARD_NAMING_SCREEN_H
